package com.browan.constellation;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.browan.constellation.data.RSSData;
import com.browan.constellation.util.Util;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class RssSaxParser extends DefaultHandler
{
	private RSSData item;
	private StringBuilder buffer;
	private Stack<String> tags;

	private List<RSSData> list;

	public RssSaxParser()
	{
		list = new ArrayList<RSSData>();
		tags = new Stack<String>();
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException
	{
		String text = new String(ch, start, length);
		if (text.length() > 0)
		{
			buffer.append(text);
		}
	}

	private String uptime;

	@Override
	public void endElement(String uri, String localName, String name) throws SAXException
	{
		try
		{
			if (null == tags || tags.size() <= 1)
			{
				return;
			}
			String node = tags.pop();
			String value = buffer.toString();

			boolean isNewsNode = tags.peek().equals("item");
			if (node.equals("title") && isNewsNode)
			{
				item.setTitle(value.trim());
			}
			else if (node.equals("link") && isNewsNode)
			{
				item.setLink(value);
			}
			else if (node.equals("category") && isNewsNode)
			{
				item.setCategory(value);
			}// pubDate
			else if (node.equals("pubDate") && isNewsNode)
			{
				item.setPubDate(value);
			}
			else if (node.equals("pubDate") && !isNewsNode)
			{

				uptime = Util.getDate(value);
			}
			else if (node.equals("description") && isNewsNode)
			{
				item.setDescription(value.trim());

			}
			else if (node.equals("item"))
			{
				if (item != null)
				{
					list.add(item);
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
	{
		if (localName.equals("channel"))
		{
		}
		else if (localName.equals("item"))
		{
			item = new RSSData();
			item.setUptime(uptime);
		}
		tags.push(localName);
		buffer = new StringBuilder();
	}

	public List<RSSData> getRss()
	{
		RSSData.CURRENT_RSS_UPTIME = uptime;
		return list;
	}

	public Bitmap downloadImage(String fileUrl)
	{
		URL myFileUrl = null;
		Bitmap bmImg = null;
		InputStream is = null;
		try
		{
			myFileUrl = new URL(fileUrl);
			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
			conn.setRequestMethod("GET");
			conn.setDoInput(true);
			conn.connect();
			is = conn.getInputStream();
			bmImg = BitmapFactory.decodeStream(is);
			return bmImg;
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (is != null)
			{
				try
				{
					is.close();
					is = null;
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
		return null;
	}
}